﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dapper_Core.BLL.Impl;
using Dapper_Core.BLL.Interface;
using Dapper_Core.Common;
using Dapper_Core.Model;
using Dapper_Core.Table;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;

namespace Dapper_Core.Web.Areas.Sys.Controllers
{
    public class Manufacturer_InfoController : Controller
    {
        private readonly IManufacturer_InfoBusiness manuInfoBusiness;
        public Manufacturer_InfoController(IManufacturer_InfoBusiness manuInfoBusiness)
        {
            this.manuInfoBusiness = manuInfoBusiness;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult GetPageList(int rowsPerPage, int offsetRows, string sortName, string sortOrder, Manufacturer_Info manufacturer)
        {
            try
            {
                if (rowsPerPage < 1 || offsetRows < 0)
                {
                    return Json(new { total = 0, rows = new List<Manufacturer_Info>() });
                }
                (List<Manufacturer_Info> roles, int count) = manuInfoBusiness.GetPageList(offsetRows / rowsPerPage, rowsPerPage, manufacturer, sortName + " " + sortOrder);
                return Json(new { total = count, rows = roles });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "获取获取本地签名服务器厂商" +
                    "分页信息失败", $"rowsPerPage={rowsPerPage}，offsetRows={offsetRows}，manufacturer={JsonConvert.SerializeObject(manufacturer)}，sortName={sortName}，sortOrder={sortOrder}", ex, "DapperCore");
                return Json(new { total = 0, rows = new List<Manufacturer_Info>() });
            }
        }

        [HttpPost]
        public IActionResult Add(Manufacturer_Info manufacturer)
        {
            try
            {
                if (manuInfoBusiness.IsExist(manufacturer.ManufacturerCode, manufacturer.ManufacturerName))
                {
                    return Json(new { Success = false, Message = "本厂商已存在，请修改厂商编码和厂商名称！" });
                }
                #region 完善Manufacturer_Info
                manufacturer.Id = Guid.NewGuid().ToString();
                manufacturer.Creator = SessionUtility.GetBasicUserInfo().UserName;
                manufacturer.CreateDate = DateTime.Now;
                manufacturer.Remarks = "无";
                #endregion
                bool success = manuInfoBusiness.Add(manufacturer);
                if (success)
                {
                    return Json(new { Success = true, Message = "添加成功！" });
                }
                return Json(new { Success = false, Message = "添加失败，请联系管理员！" });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "本地签名服务器厂商添加失败", "manufacturer=" + JsonConvert.SerializeObject(manufacturer), ex, "DapperCore");
                return Json(new { Success = false, Message = "添加失败，请联系管理员！" });
            }
        }

        [HttpPost]
        public IActionResult Edit(Manufacturer_Info manufacturer)
        {
            try
            {
                #region 完善Manufacturer_Info
                Manufacturer_Info old = manuInfoBusiness.Get(manufacturer.Id);
                old.ManufacturerName = manufacturer.ManufacturerName;
                old.ManufacturerType = manufacturer.ManufacturerType;
                old.ManagerEmail = manufacturer.ManagerEmail;
                old.ManagerPhone = manufacturer.ManagerPhone;
                old.IsActive = manufacturer.IsActive;
                old.Modifier = SessionUtility.GetBasicUserInfo().UserName;
                old.ModifyDate = DateTime.Now;
                #endregion
                bool success = manuInfoBusiness.UpdateNotNull(old);
                if (success)
                {
                    return Json(new { Success = true, Message = "修改成功！" });
                }
                return Json(new { Success = false, Message = "修改失败，请联系管理员！" });
            }
            catch (Exception ex)
            {
                NlogUtility.WriteError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.FullName, "本地签名服务器厂商修改失败", "manufacturer=" + JsonConvert.SerializeObject(manufacturer), ex, "DapperCore");
                return Json(new { Success = false, Message = "修改失败，请联系管理员！" });
            }
        }
    }
}